package com.blog.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.blog.bean.MasterBean;
import com.blog.db.DB;

public class LogonDao
{
	private DB connection = null;

	public LogonDao()
	{
		connection = new DB();
	}
	
	public MasterBean getMaster(MasterBean logoner)
	{
		MasterBean master=null;
//		String sql="select * from tb_master";
		String sql="select * from tb_master where master_name='"+logoner.getMasterName()+"' and master_password='"+logoner.getMasterPass()+"'";
		ResultSet rs=connection.executeQuery(sql);
		try
		{
			if(rs!=null&&rs.next())
			{
				master=new MasterBean();	
				master.setMasterId(rs.getInt(1));
				master.setMasterName(rs.getString(2));
				master.setMasterSex(rs.getString(4));
				master.setMasterOicq(rs.getString(5));
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return master;
	}
	public MasterBean getMaster(int uid)
	{
		MasterBean master=null;
		String sql="select * from tb_master where master_id="+uid;
		ResultSet rs=connection.executeQuery(sql);
		try
		{
			if(rs!=null&&rs.next())
			{
				master=new MasterBean();	
				master.setMasterId(rs.getInt(1));
				master.setMasterName(rs.getString(2));
				master.setMasterSex(rs.getString(4));
				master.setMasterOicq(rs.getString(5));
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return master;
	}
	
	public boolean logon(MasterBean logoner)
	{
		boolean mark=false;
		if(logoner!=null)
		{
			String sql="select * from tb_master where master_name='"+logoner.getMasterName()+"' and master_password='"+logoner.getMasterPass()+"'";
			ResultSet rs=connection.executeQuery(sql);
			try
			{
				if(rs!=null&&rs.next())
				{
					mark=true;
				}
				else
				{
					mark=false;
				}
			}
			catch (SQLException e)
			{
				mark=false;
				e.printStackTrace();
			}
			try
			{
				rs.close();
				connection.closed();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return mark;		
	}
}
